# coding: utf-8

# 数据索引转换
# 对数据创建新的索引使得数据操作更直观

# In[1]:

import pandas as pd

# In[2]:

fandango = pd.read_csv('fandango_score_comparison.csv')
print(type(fandango))

# In[17]:

# drop = False 保留原有的列
# drop = True 不保留原有的列
fandango_films = fandango.set_index('FILM', drop=False)
# print(fandango_films[:3])
# fandango_films[:3]


# In[11]:

# Slice using either bracket notation or loc[]
# fandango_films["Avengers: Age of Ultron (2015)":"Hot Tub Time Machine 2 (2015)"]
fandango_films.loc["Avengers: Age of Ultron (2015)":"Hot Tub Time Machine 2 (2015)"]

# In[34]:

import numpy as np

# returns the data types as a Series
types = fandango_films.dtypes
# print(types)
# filter data types to just floats, index attributes returns just column names
float_columns = types[types.values == 'float64'].index
print('---')
print(float_columns[:2])
float_df = fandango_films[float_columns]
print('---')
print(float_df[:2])

# 'x' is a Series object representing a column
# 方差，每一列计算方差
deviations = float_df.apply(lambda x: np.std(x))[:5]
print('---')
print(deviations)

# In[31]:

# 也可按行计算方差(例子数据没有实际意义)
rt_mt_user = float_df[['Metacritic_User', 'IMDB']]
rt_mt_user.apply(lambda x: np.std(x), axis=1)[:10]


# In[ ]:
